package dgut.bbm.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dgut.bbm.to.FriendTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import dgut.bbm.entity.MessageEntity;
import dgut.bbm.service.MessageService;
import dgut.bbm.common.utils.PageUtils;
import dgut.bbm.common.utils.R;



/**
 * 
 *
 * @author seventeen
 * @email 1973182107@qq.com
 * @date 2023-05-06 16:56:08
 */
@RestController
@RequestMapping("bbm/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/getFriends/{openId}")
    public R getFriends(@PathVariable("openId") String openId){
        List<FriendTo> friends=messageService.getFriends(openId);
        return R.ok().put("data",friends);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        MessageEntity message = messageService.getById(id);

        return R.ok().put("message", message);
    }
    /**
     * 分页获取我与某人的历史聊天记录
     * @param myMemberId
     * @param youMemberId
     * @return
     */
    @GetMapping("/getMessageHistory")
    public R getMessageHistory(Long myMemberId, Long youMemberId, int pageNo, int pageSize){
        Page page = new Page<>(pageNo,pageSize);
        //查询历史聊天记录
        QueryWrapper<MessageEntity> queryWrapper = new QueryWrapper();
        queryWrapper.and(wrapper -> wrapper.and(item -> item.eq("sender",myMemberId).eq("receiver",youMemberId))
                .or(item -> item.eq("receiver",myMemberId).eq("sender",youMemberId))
        );
        queryWrapper.orderByDesc("created_time");
        Page pageData = messageService.page(page, queryWrapper);
        //第一次获取，将此人与对方的聊天消息，全部置为已读
        if(pageNo==1){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("del_flag", 0);
            updateWrapper.eq("sender",youMemberId);
            updateWrapper.eq("receiver",myMemberId);
            updateWrapper.set("is_read",1);
            messageService.update(updateWrapper);
        }
        return R.ok().put("datas",pageData);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("bbm:message:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = messageService.queryPage(params);

        return R.ok().put("page", page);
    }




    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("bbm:message:save")
    public R save(@RequestBody MessageEntity message){
		messageService.save(message);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("bbm:message:update")
    public R update(@RequestBody MessageEntity message){
		messageService.updateById(message);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("bbm:message:delete")
    public R delete(@RequestBody Long[] ids){
		messageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
